/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @filename: databaseManager.h
 */
#ifndef __DATABASE_MANAGER_H__
#define __DATABASE_MANAGER_H__


#include <index.h>
#include <error.h>
#include <util.h>
#include <mm_handler.h>

class Table;
class Identifier;
/**
* @class DatabaseManager
*
* @brief Interface for database management operations.
* This manages all the database objects. Currently it supports two database <br>
* objects namely table and index.<br/>
* <br/>
* Functionality: <br/>
*     1.Table Management (create, drop, open and close) <br/>
*     2.Index Management (create and drop) <br/>
* <br/>
*
*/
class DatabaseManager
{
public:
    DatabaseManager() {}
    /** creates a table in the database
    *   @param name name of the table
    *   @param def table definition
    *   @return DbRetVal
    */
    virtual BackEnd::DbRetVal createTable(const char *name, Table& def,
                                          HA_CREATE_INFO *create_info,
                                          bool internal_table = 0)=0;

    /** deletes a table from the database
    *   @param name name of the table
    *   @return DbRetVal
    */
    virtual BackEnd::DbRetVal dropTable(const char *name)  =0;
    /*
     *  @param  name char*      the table name
     *  @param  _len size_t     the table name length
     */
    virtual BackEnd::DbRetVal checkTableExists(const char *name,bool b_getlock=true) =0;
    /** opens a table for processing
    *   @param name name of the table
    *   @return DbRetVal
    */
    virtual int openTable(const char *name,Table* tbl, bool b_creatIdx = true)=0;

    /** closes the table handle passed
    *   @param table handle to the table
    */
    virtual void closeTable     (Table* table)=0;


    /** creates an index on the specified table. <br/>
    *   Create appropriate derived class object of IndexInfo based on the type of <br/>
    *   the index created and pass it to this method.
    *   @param indName index name
    *   @param info IndexInfo
    */
    virtual BackEnd::DbRetVal createIndex(const char *indName,
                                          Table&, IndexInfo*, bool)=0;

    /** deletes the index object
    *   @param name index name
    */
    virtual BackEnd::DbRetVal dropIndex(const char *name, void*, bool)=0;


    virtual BackEnd::DbRetVal disableIndex(Table& tbl, void* tptr)=0;


    virtual BackEnd::DbRetVal enableIndex(Table& tbl, void* tptr) = 0;

    virtual BackEnd::DbRetVal is_indexes_are_disabled(Table& tbl, void* tptr)= 0;

    virtual BackEnd::DbRetVal newRecord(Table& tbl,const unsigned char *record)=0;

    virtual BackEnd::DbRetVal delete_all_rows(Table& tbl) = 0;

    virtual void getInfo(MM_INFO* mminfo) = 0;

    virtual int mm_rkey(Table& tbl,MM_INFO* mminfo, unsigned char *record, int inx,
                        const unsigned char *key,
                        key_part_map keypart_map, enum ha_rkey_function find_flag) = 0;

    virtual int mm_rfirst(Table& tbl,MM_INFO* mminfo,unsigned char *record, int inx)=0;

    virtual int mm_rnext (Table& tbl,MM_INFO* mminfo,unsigned char *record, int inx)=0;

    virtual int mm_rnext_same(Table& tbl,MM_INFO* mminfo,unsigned char *buf)=0;

    virtual BackEnd::DbRetVal delete_row(Table& tbl,MM_INFO* mminfo,
                                         unsigned char *record) = 0;
    /**
     *  The destructor
     */
    virtual ~DatabaseManager(){}

    friend class Table;
    friend class Session;
    friend class mm_handler;
};

#endif  //End of DatabaseManager
